﻿using PetaPoco;
using QDSpider.database;
using QDSpider.Models;

namespace QDSpider.manager
{
    public static class ArticleManager
    {
        public static  Page<Article> GetPage(int userid,int tagid, int state, string title, int page,int size,int createuserid)
        {
            using (var db = new S003Database())
            {
                var sql=new Sql();
                sql.Select("*");
                sql.From("vIFO_Article");
                if (state > 0)
                    sql.Where("state=@0", state);
                if (tagid > 0)
                    sql.Where("tag_id=@0", tagid);
                if (createuserid > 0)
                    sql.Where("create_userid=@0", createuserid);
                if (userid > 0)
                    sql.Where($"id not in(select article_id from vifo_Product where UserID={userid})");
                if (!string.IsNullOrEmpty(title))
                    sql.Where($"title like '%{title}%'");
                sql.OrderBy("id desc");
                return  db.Page<Article>(page, size,sql);
            }
        }
        /// <summary>
        /// 获取文章列表，不带详细内容
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="tagid"></param>
        /// <param name="state"></param>
        /// <param name="title"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="createuserid"></param>
        /// <returns></returns>
        public static  Page<LightArticle> GetLightPage(int userid, int tagid, int state, string title, int page, int size, int createuserid)
        {
            
            using (var db = new S003Database())
            {
                var sql = new Sql();
                sql.Select("[id],[title],[logo],[state],[date],[source],[sourceid],[htmlurl],[html_date],[push_count],[tag_name],[tag_id],[read_num],[like_num],[create_userid],[create_username],[copyright]");
                sql.From("vIFO_Article");
                
                if (tagid > 0)
                    sql.Where("tag_id=@0", tagid);
                if (state > 0)
                    sql.Where("state=@0", state);
                if (createuserid > 0)
                    sql.Where("create_userid=@0", createuserid);
                if (userid > 0)
                    sql.Where($"id not in(select article_id from vifo_Product where UserID={userid})");
                if (!string.IsNullOrEmpty(title))
                    sql.Where($"title like '%{title}%'");
                sql.OrderBy("id desc");
                return  db.Page<LightArticle>(page, size, sql);
            }
        }
        public static  Article Get(int id)
        {
            using (var db = new S003Database())
            {
                return  db.Single<Article>(id);
            }
        }

        public static bool Exist(string title, int userid)
        {
            using (var db=new S003Database())
            {
                var id = db.ExecuteScalar<object>("select id from vIFO_Article where title=@0 and create_userid=@1 ", title,
                    userid);
                return id != null;
            }
        }

        public static  int CountTag(int tagid)
        {
            if (tagid < 0) return 0;
            using (var db = new S003Database())
            {
                return  db.ExecuteScalar<int>($"select count(*) from vIFO_Article where tag_id={tagid} and state=1");
            }
        }

        public static int Insert(Article ar)
        {
            using (var db = new S003Database())
            {
                var t= db.Insert(ar);
                return (int) t;
            }
        }

        public static  int Verify(int id, int state)
        {
            using (var db = new S003Database())
            {
                return  db.Execute("update vIFO_Article set state=@0 where id=@1 ",state,id);
            }
        }
    }
}
